<?php


namespace app\api\controller;


use addon\membersignin\model\Signin;
use addon\wechat\model\Wechat;
use app\model\member\MemberSignin as MemberSigninModel;
use app\model\web\Config as ConfigModel;
use Pili\Hub;
use Qiniu\Auth;
use Qiniu\Credentials;
use Qiniu\Pili\Client;
use Qiniu\Pili\Mac;
use Qiniu\Rtc\AppClient;
use think\facade\Db;
use function Qiniu\Pili\RTMPPublishURL;

class Qiniu extends BaseApi
{
    protected $accessKey = 'l9OPt04UzXChD6RhmndKDGkH1hgy1Vzsuzoe6y1u';
    protected $secretKey = 'gdW3hIKjjG8wFNDYL2FvedrnNH_jCPpj7-UJ6wwE';
    protected $appId = 'hl5phhlol';
    //生成加入房间的令牌

    public function getRoomToken($room_id,$member_id,$permission = 'user')
    {
        $accessKey = $this->accessKey;
        $secretKey = $this->secretKey;
        $auth = new Auth($accessKey, $secretKey);
        $client = new AppClient($auth);
        $appId = $this->appId;
        //生成房间名称
        $roomName = 'room'.($room_id + 10000);
        $userId = 'member'.($member_id + 10000);
        $expireAt = time()+3600;
        // 生成加入房间的令牌 RoomToken
        $RoomToken = $client->appToken($appId, $roomName, $userId, $expireAt, $permission);
        return $RoomToken;
    }

    //创建应用
    //删除应用
    //获取应用当前配置
    public function getApp()
    {
        $accessKey = $this->accessKey;
        $secretKey = $this->secretKey;
        $auth = new Auth($accessKey, $secretKey);
        $client = new AppClient($auth);
        $appId = $this->appId;
        list($ret, $err)  = $client->getApp($appId);
        if ($err !== null) {
            var_dump($err);
        } else {
            var_dump($ret);
        }
    }

    //获取rtmp地址
    //domain 直播空间绑定的 RTMP 直播域名,hub直播空间名称,streamKey直播流名称,
    public function getRtmp($hub, $streamKey)
    {
        require_once app()->getRootPath().'extend/pili_sdk/lib/Pili_v2.php';
        $accessKey = $this->accessKey;
        $secretKey = $this->secretKey;
        $url = RTMPPublishURL("pili-live-rtmp.live.xing-peng.com", $hub, $streamKey, 3600, $accessKey, $secretKey);
        return $url;

    }

    //直播列表
    public function broadcastList()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $page = input('page', 1);
        $page_size = input('page_size', PAGE_LIST_ROWS);
        $search = $this->params['search'] ?? '';
        //正在直播的在上面，下面是直播预告
        $map = [];
        $map[] = ['is_show', '=', 1];
        $map[] = ['status', '=', 1];
        if ($search) {
            $map[] = ['title', 'like', "%$search%"];
        }
        $is_index = model('member_broadcast')->getList($map,'*','sort desc, id desc');
        foreach ($is_index as $key => $value) {
            $is_index[$key]['start_time'] = date('Y-m-d H:i', $value['start_time']);
        }
        //最新预告
        $map = [];
        $map[] = ['is_show', '=', 1];
        $map[] = ['status', '=', 0];
        if ($search) {
            $map[] = ['title', 'like', "%$search%"];
        }
        $list = model('member_broadcast')->pageList($map,'*','sort desc, id desc',$page,$page_size);
        if (isset($list['list'])) {
            foreach ($list['list'] as $key => $value) {
                $list['list'][$key]['start_time'] = date('Y-m-d H:i', $value['start_time']);
                $user_log = model('member_broadcast_user')->getInfo(['broadcast_id'=>$value['id'],'member_id'=>$this->member_id]);
                $list['list'][$key]['log'] = $user_log?$user_log:[];
            }
        }

        return (new Api())->responseSuccess(['index' => $is_index, 'list' => $list]);
    }

    public function objectToArray($obj) {
        if (is_object($obj)) {
            $arr = array();
            $keys = get_object_vars($obj);
            foreach ($keys as $key => $value) {
                $arr[$key] = $this->objectToArray($value);
            }
            return $arr;
        } else if (is_array($obj)) {
            $arr = array();
            foreach ($obj as $key => $value) {
                $arr[$key] = $this->objectToArray($value);
            }
            return $arr;
        } else {
            return $obj;
        }
    }



    //直播详情
    public function broadcastDetail()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id'];
        //$inviter_id = $this->params['inviter_id'];
        $res = model('member_broadcast')->getInfo(['id' => $id, 'is_show' => 1]);
        if (!$res) return (new Api())->responseError('该直播不存在');
        /*if (isset($res['rule_json'])) {
            $rule = json_decode($res['rule_json'], true);
            $new_rule_json = $this->objectToArray($rule);

            $res['rule'] = array_values($new_rule_json);
        }*/
        //判断用户是否有权限观看
        $member = model('member')->getInfo(['member_id' => $this->member_id]);
        if ($member['member_level'] < $res['member_level']) {
            return (new Api())->responseError('会员权限不足，暂不可观看');
        }
        //是否是主播
        $res['roomToken'] = '';
        if ($this->member_id == $res['member_id']) {
            $res['is_admin'] = 1;
        } else {
            $res['is_admin'] = 0;
            //判断直播是否开启
            if ($res['status'] == 1) {
                $res['roomToken'] = $this->getRoomToken($id, $this->member_id, 'user');
                //记录直播观看的人
                /*$user = model('member_broadcast_user')->getInfo(['broadcast_id'=>$id,'member_id'=>$this->member_id]);
                if ($user){
                    if ($user->is_remove==1){
                        return (new Api())->responseError('被踢出直播间，暂不可观看');
                    }
                }*/
                /*if (!$user) {
                    model('member_broadcast_user')->add(['inviter_id'=>$inviter_id,'broadcast_id'=>$id,'member_id'=>$this->member_id,'create_time'=>time()]);
                }*/
            }
        }
        $res['username'] = $member['username'] ?? '';
        $res['nickname'] = $member['nickname'] ?? '';
        $res['headimg'] = $member['headimg'] ?? '';
        //返回主播头像昵称
        $member = model('member')->getInfo(['member_id' => $res['member_id']]);
        $res['admin_username'] = $member['username'] ?? '';
        $res['admin_nickname'] = $member['nickname'] ?? '';
        $res['admin_headimg'] = $member['headimg'] ?? '';
        //返回主播userid
        $res['admin_userid'] = 'member'.($res['member_id'] + 10000);
        //返回直播间商品
        $sku_ids = $res['sku_ids'];
        $sku_ids = explode(',',$sku_ids);
        $condition = [
            ['g.goods_state', '=', 1],
            ['g.is_delete', '=', 0],
            ['g.site_id', '=', $this->site_id ],
            ['gs.sku_id', 'in', $sku_ids ],
        ];
        $field = 'gs.goods_id,gs.sku_id,gs.price,gs.market_price,gs.discount_price,g.goods_stock,(g.sale_num + g.virtual_sale) as sale_num,g.goods_name,gs.site_id,gs.is_free_shipping,g.goods_image,gs.is_virtual,g.recommend_way,gs.unit,gs.promotion_type,g.label_name,g.keywords,g.goods_class';
        if ($token[ 'code' ] >= 0) {
            $field .= ',gs.is_consume_discount,gs.discount_config,gs.member_price,gs.discount_method';
        }
        $alias = 'gs';
        $join = [
            ['goods g', 'gs.sku_id = g.sku_id', 'inner']
        ];
        $config_model = new ConfigModel();
        $sort_config = $config_model->getGoodsSort($this->site_id);
        $sort_config = $sort_config['data']['value'];

        $order_by = 'g.sort ' . $sort_config['type'] . ',g.create_time desc';
        $goods = model('goods_sku')->getList($condition, $field, $order_by,$alias,$join);
        $res['goods'] = $goods;

        return (new Api())->responseSuccess($res);
    }
    
    //直播上链接商品
    public function broadcastGoods()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id'];
        $res = model('member_broadcast')->getInfo(['id' => $id, 'is_show' => 1]);
        if (!$res) return (new Api())->responseError('该直播不存在');
        //获取直播商品
        $sku_ids = $res['sku_ids'];
        $sku_ids = explode(',',$sku_ids);
        $condition = [
            ['g.goods_state', '=', 1],
            ['g.is_delete', '=', 0],
            ['g.site_id', '=', $this->site_id ],
            ['gs.sku_id', 'in', $sku_ids ],
        ];
        $field = 'gs.goods_id,gs.sku_id,gs.price,gs.market_price,gs.discount_price,g.goods_stock,(g.sale_num + g.virtual_sale) as sale_num,g.goods_name,gs.site_id,gs.is_free_shipping,g.goods_image,gs.is_virtual,g.recommend_way,gs.unit,gs.promotion_type,g.label_name,g.keywords,g.goods_class';
        if ($token[ 'code' ] >= 0) {
            $field .= ',gs.is_consume_discount,gs.discount_config,gs.member_price,gs.discount_method';
        }
        $alias = 'gs';
        $join = [
            ['goods g', 'gs.sku_id = g.sku_id', 'inner']
        ];
        $config_model = new ConfigModel();
        $sort_config = $config_model->getGoodsSort($this->site_id);
        $sort_config = $sort_config['data']['value'];

        $order_by = 'g.sort ' . $sort_config['type'] . ',g.create_time desc';
        $goods = model('goods_sku')->getList($condition, $field, $order_by,$alias,$join);
        return  (new Api())->responseSuccess($goods);


    }

    //开始直播
    public function broadcastStart()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id'];
        $res = model('member_broadcast')->getInfo(['id' => $id, 'is_show' => 1]);
        if (!$res) return (new Api())->responseError('请确认id');
        //判断直播权限
        if ($this->member_id != $res['member_id']) {
            return (new Api())->responseError('没有权限');
        }
        //生成roomToken和地址,直播流和房间名一样
        $roomToken = $this->getRoomToken($id, $this->member_id, 'admin');
        $rtmp = $this->getRtmp('zhiboxingpeng', 'room'.($id + 10000));
        return (new Api())->responseSuccess(['roomToken' => $roomToken, 'rtmp' => $rtmp]);
    }

    //直播是否开启成功/关闭直播
    public function broadcastStatus()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id'];
        $type = $this->params['type'];
        if (!in_array($type, ['1','2'])) return (new Api())->responseError('请传递type');
        $res = model('member_broadcast')->getInfo(['id' => $id, 'is_show' => 1]);
        if (!$res) return (new Api())->responseError('该直播不存在');
        //判断直播权限
        if ($this->member_id != $res['member_id']) {
            return (new Api())->responseError('没有权限');
        }
        if ($type == 1) { //开启直播
            model('member_broadcast')->update(['status' => 1],['id' => $id]);
            //开启直播推送模板消息
            $model = model('member_broadcast')->getInfo(['id'=>$id]);
            if ($model['is_send'] == 0) {
                model('member_broadcast')->update(['is_send' => 1],['id' => $id]);
                $member = model('member')->getList(['site_id'=>$this->site_id,'is_delete'=>0]);
                foreach ($member as $key => $value) {
                    if (!empty($value['wx_openid']) && $value['member_id'] != $this->member_id) {
                        $data2 = [
                            'openid' => $value['wx_openid'], //openid
                            'template_id' => 'GkL_sI6bG9kV2XEquagawKAn4VzyGGEZBBSnyuE6NsU', //模板id
                            'url' => 'https://zhibo1.yixianwenhua.com/h5/otherpages/thirdLive/thirdLive', //跳转链接
                            'miniprogram' => '', //跳转小程序
                            'data' => [
                                'first' => [
                                    'value' => '开播提醒',
                                ],
                                'keyword1' => [
                                    'value' => $res['title'], //商品名称
                                ],
                                'keyword2' => [
                                    'value' => date('Y-m-d H:i'),
                                ],
                                'keyword3' => [
                                    'value' => '谦赋文化',
                                ],
                                'remark' => [
                                    'value' => '请及时观看!',
                                ]
                            ], //模板变量
                        ];
                        $wechat = new Wechat(1);
                        //$wechat->sendTemplateMessage($data2);
                    }
                }
            }
        } else { //关闭直播
            model('member_broadcast')->update(['status' => 2],['id' => $id]);
        }
        return (new Api())->responseSuccess('');
    }

    //申请连麦
    public function connectAdd()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['room_id'];
        $res = model('member_broadcast')->getInfo(['id' => $id, 'is_show' => 1]);
        if (!$res) return (new Api())->responseError('该直播不存在');
        if ($res['status'] != 1) {
            return (new Api())->responseError('该直播未开始');
        }
        $count = model('member_connect')->getInfo([['site_id','=',$this->site_id],['member_id','=',$this->member_id],['room_id','=',$id],['status','<>',2]]);
        if (!$count) {
            $res = model('member_connect')->add([
                'site_id' => $this->site_id,
                'member_id' => $this->member_id,
                'room_id' => $id,
                'create_time' => time(),
            ]);
        } else {
            $res = $count['id'];
        }
        return (new Api())->responseSuccess(['id' => $res]);
    }

    //取消/挂断
    public function connectCancel()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id']; //申请的id
        $res = model('member_connect')->getInfo([['id','=',$id]]);
        if (!$res) return (new Api())->responseError('该记录不存在');
        model('member_connect')->update(['status' => 2],['id' => $id]);
        return (new Api())->responseSuccess('');
    }
    
    //同意连麦
    public function connectAgree()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id']; //申请的id
        $res = model('member_connect')->getInfo([['id','=',$id]]);
        if (!$res) return (new Api())->responseError('该记录不存在');
        model('member_connect')->update(['status' => 1],['id' => $id]);
        return (new Api())->responseSuccess('');
    }
    
    //连麦列表
    public function connectList()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['room_id'];
        $res = model('member_broadcast')->getInfo(['id' => $id, 'is_show' => 1]);
        if (!$res) return (new Api())->responseError('该直播不存在');
        $res = model('member_connect')->getList([['site_id','=',$this->site_id],['room_id','=',$id],['status','in',['0','1']]]);
        foreach ($res as $key => $value) {
            $member = model('member')->getInfo(['member_id' => $value['member_id']]);
            $res[$key]['nickname'] = $member['nickname'] ?? '';
            $res[$key]['headimg'] = $member['headimg'] ?? '';
        }
        return (new Api())->responseSuccess($res);
    }
    
    //申请记录详情
    public function connectDetail()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id']; //申请的id
        $res = model('member_connect')->getInfo([['id','=',$id],['member_id','=',$this->member_id]]);
        if (!$res) return (new Api())->responseError('该记录不存在');
        $member = model('member')->getInfo(['member_id' => $res['member_id']]);
        $res['nickname'] = $member['nickname'] ?? '';
        $res['headimg'] = $member['headimg'] ?? '';
        return (new Api())->responseSuccess($res);
    }

    //回放
    public function saveas()
    {
        require_once app()->getRootPath().'extend/pili_sdk/lib/Pili_v2.php';
        $accessKey = $this->accessKey;
        $secretKey = $this->secretKey;

        $mac = new Mac($accessKey, $secretKey);
        $client = new Client($mac);
        $hubName = "zhiboxingpeng"; //直播空间名称
        $hub = $client->hub($hubName);
        $streamKey = "room10069"; // 流名
        $stream = $hub->stream($streamKey);
        $fname = $stream->saveas(array("format" => "mp4"));

        dd($fname);
    }

    public function ceshi(){
        require_once app()->getRootPath().'extend/pili_sdk/lib/Pili_v2.php';

        $accessKey = $this->accessKey;
        $secretKey = $this->secretKey;
        $mac = new Mac($accessKey, $secretKey);
        $client = new Client($mac);
        $hubName = "zhiboxingpeng"; //直播空间名称
        $hub = $client->hub($hubName);
        print_r($hub);
        //$streamKey = "php-sdk-test" . time();
        $streamKey = "room10090";
        $stream = $hub->stream($streamKey);
        print_r($stream);
        try {
            //echo "================Create stream\n";
            //$resp = $hub->create($streamKey);
            //print_r($resp);

            echo "================Save stream:\n";
            $resp = $stream->saveas(array("format" => "mp4","start"=>0,'end'=>0));

            print_r($resp);
        } catch (\Exception $e) {
            echo "Error:", $e->getMessage(), "\n";
        }
    }

    //直播历史记录
    //直播列表
    public function historyList()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $page = input('page', 1);
        $page_size = input('page_size', PAGE_LIST_ROWS);
        $search = $this->params['search'] ?? '';
        //结束的直播
        $map = [];
        $map[] = ['is_show', '=', 1];
        $map[] = ['status', '=', 2];
        if ($search) {
            $map[] = ['title', 'like', "%$search%"];
        }
        $list = model('member_broadcast')->pageList($map,'*','sort desc, id desc',$page,$page_size);
        if (isset($list['list'])) {
            foreach ($list['list'] as $key => $value) {
                $list['list'][$key]['start_time'] = date('Y-m-d H:i', $value['start_time']);
            }
        }
        return (new Api())->responseSuccess($list);
    }
    
    //直播历史详情
    public function historyDetail()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id'];
        $info = model('member_broadcast')->getInfo(['id'=>$id]);
        if (!$info) return (new Api())->responseError('该记录不存在');

        if (empty($info['history_video_url'])) { //生成记录
            require_once app()->getRootPath().'extend/pili_sdk/lib/Pili_v2.php';
            $accessKey = $this->accessKey;
            $secretKey = $this->secretKey;
            $mac = new Mac($accessKey, $secretKey);
            $client = new Client($mac);
            $hubName = "zhiboxingpeng"; //直播空间名称
            $hub = $client->hub($hubName);

            $streamKey = 'room'.($id + 10000); // 流名
            $stream = $hub->stream($streamKey);
            $fname = $stream->saveas(array("format" => "mp4"));

            if (!empty($fname['fname'])) {
                model('member_broadcast')->update(['history_video_url'=>$fname['fname']],['id'=>$id]);
                $info['history_video_url'] = $fname['fname'];
            }
        }
        if ($info['history_video_url']) {
            //返回直播域名
            $info['history_video_url'] = 'http://live.xing-peng.com/'.$info['history_video_url'];
        }
        return (new Api())->responseSuccess($info);
    }

    /**
     * 列出指定房间下当前所有用户
     */
    public function listUser()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $room_id = $this->params['room_id'] ?? 0;
        $roomName = 'room'.($room_id+10000); //房间名称
//        $res = model('member_broadcast')->getInfo(['id' => $room_id, 'is_show' => 1]);
//        if (!$res) return (new Api())->responseError('该直播不存在');
        $accessKey = $this->accessKey;
        $secretKey = $this->secretKey;
        $auth = new Auth($accessKey, $secretKey);
        $client = new AppClient($auth);
        $appId = $this->appId;
        list($ret, $err)  = $client->listUser($appId, $roomName);

        if (empty($ret['users'])) { //默认0人
            $count = 0;
        } else {
            $count = count($ret['users']);
        }

        $config_model = new Signin();
        $config_result = $config_model->getConfig($this->site_id);

        //$member_signin = new MemberSigninModel();
        //$is_sign          = $member_signin->isSign($token['data']['member_id']);
        $sign_info = model('signin_task')->getInfo(['broadcast_id'=>$room_id,'is_delete'=>0]);
        if ($sign_info){
            $member_sign_info = model('member_signin')->getInfo(['signin_id'=>$sign_info['id']]);
            $is_sign = $member_sign_info?1:0;
        } else {
            $is_sign = 0;
        }
        $wait_time = model('member_broadcast_user')->getInfo(['broadcast_id'=>$room_id,'member_id'=>$this->member_id]);
        $data = [
            'count'=>$count,
            'sign_config'=>$config_result['data']['is_use'],
            'is_sign'=>$is_sign,
            'sign_info'=>$sign_info?$sign_info:[],
            'wait_time'=>$wait_time?$wait_time:[]
        ];
        return (new Api())->responseSuccess($data);
    }


    /**
     * Notes:记录在直播间待的时长
     * User: gai
     * Date: 2023/12/28
     * Time: 14:30
     */
    public function recordDuration()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id'];
        $begin_time = $this->params['begin_time'];
        $end_time = $this->params['end_time'];
        $duration = $end_time-$begin_time;
        //$duration = $this->params['duration'];
        $inviter_id = $this->params['inviter_id'];

        $is_remove = isset($this->params[ 'is_remove' ]) ? $this->params[ 'is_remove' ] : 0;
        /*$user = model("member_broadcast_user")->getInfo([ [ 'broadcast_id', '=', $id ], [ 'member_id', '=', $this->member_id ] ], 'id, duration');
        if (!empty($user)) {
            $res = model("member_broadcast_user")->update([ 'duration' => $user[ 'duration' ] + $duration ,'is_remove'=>$is_remove], [ [ 'id', '=', $user[ 'id' ] ] ]);
        } else {*/
            $data = [
                'inviter_id'=>$inviter_id,
                'broadcast_id'=>$id,
                'member_id'=>$this->member_id,
                'create_time'=>time(),
                'is_remove'=>$is_remove
            ];
            $res = model("member_broadcast_user")->add($data);
        //}

        return $this->response($this->success($res));
    }

    //主播发布签到任务
    public function signinTask(){
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $broadcast_id = isset($this->params['broadcast_id']) ? $this->params['broadcast_id'] : 0;
        $signin_time = isset($this->params['signin_time']) ? $this->params['signin_time'] : 0;
        $info = model('signin_task')->getInfo(['broadcast_id'=>$broadcast_id,'is_delete'=>0]);
        if ($info)model('signin_task')->update(['is_delete'=>1],[['broadcast_id','=',$broadcast_id]]);
        $data = [
            'member_id'=>$this->member_id,
            'signin_time'=>$signin_time,
            'broadcast_id'=>$broadcast_id,
            'addtime'=>time()
        ];

        $res = model('signin_task')->add($data);
        if ($res){

            return $this->response($this->success($res));
        } else {
            return $this->response($this->error('','发布失败'));
        }
    }

    //用户签到
    public function signin(){
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $signin_id = isset($this->params['signin_id']) ? $this->params['signin_id'] : 0;

        $data = [
            'member_id'=>$this->member_id,
            'signin_id'=>$signin_id,
            'sign_time'=>time()
        ];

        $res = model('member_signin')->add($data);
        if ($res){
            return $this->response($this->success($res));
        } else {
            return $this->response($this->error('','签到失败'));
        }
    }


    //领取红包
    public function receiveReward()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = isset($this->params['id']) ? $this->params['id'] : 0;
        $reward = isset($this->params['reward']) ? $this->params['reward'] : 0;
        $time = isset($this->params['time']) ? $this->params['time'] : 0;
        $remark = '观看时长大于'.$time.'秒奖励';
        $member_account_model = new \app\model\member\MemberAccount();
        $res = $member_account_model->addMemberAccount($this->site_id, $this->member_id, 'balance_money', $reward, 'reward', $id, $remark);;
        return $this->response($res);
    }

    public function rewardDetail()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $id = $this->params['id'];
        //$inviter_id = $this->params['inviter_id'];
        $res = model('member_broadcast')->getInfo(['id' => $id, 'is_show' => 1],'rule_json');
        if (!$res) return (new Api())->responseError('该直播不存在');
        if (isset($res['rule_json'])) {
            $rule = json_decode($res['rule_json'], true);
            $new_rule_json = $this->objectToArray($rule);
            $rule = array_values($new_rule_json);
            foreach ($rule as $k=>$v){
                $detail = model('member_account')->getInfo([
                    'member_id'=>$this->member_id,
                    'from_type'=>'reward',
                    'account_type'=>'balance_money',
                    'type_tag'=>$id,
                    'remark'=>'观看时长大于'.$v['limit'].'秒奖励'
                ]);

                if ($detail)unset($rule[$k]);
            }

            $res['rule'] = array_values($rule);
        }
        return (new Api())->responseSuccess($res);
    }
}